#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define TIME (1e3*clock()/CLOCKS_PER_SEC)
#define debug(fmt,...) fprintf(stderr, fmt, ##__VA_ARGS__)
#define debugln(fmt,...) fprintf(stderr, "[%d] " fmt, __LINE__,##__VA_ARGS__)
bool memBeg;
char fetch() {
    return getchar();
}
int readint() {
    char c=fetch(); int ret=0,neg=1;
    for(;c<'0'||c>'9';c=fetch()) if(c=='-') neg=-1;
    for(;'0'<=c&&c<='9';c=fetch()) ret=ret*10+(c&15);
    return ret*neg;
}
template<typename T> void chkmax(T &x,T y) {if(x<y) x=y;}
template<typename T> void chkmin(T &x,T y) {if(x>y) x=y;}
constexpr int A=1e6+5;
constexpr ll INF=0x3f3f3f3f3f3f3f3f;
void mian() {
    int n=readint();
    vector<int> a(n+5);
    vector<ll> sum(n+5),maxx[2],hlp[2];
    maxx[0].assign(n+5,0);
    maxx[1].assign(n+5,0);
    hlp[0].assign(A+5,-INF);
    hlp[1].assign(A+5,-INF);
    for(int i=1;i<=n;i++) a[i]=readint();
    for(int i=2;i<=n+1;i++) sum[i]=sum[i-1]+(a[i]==a[i-1])*a[i];
    hlp[0][0]=hlp[1][0]=0;
    ll hlp0=0,hlp1=0;
    for(int i=1;i<=n;i++) {
        chkmax(maxx[0][i],hlp[1][a[i+1]]+a[i+1]+sum[i]);
        chkmax(maxx[0][i],hlp1+sum[i]);
        chkmax(maxx[1][i],hlp[0][a[i+1]]+a[i+1]+sum[i]);
        chkmax(maxx[1][i],hlp0+sum[i]);
        chkmax(hlp[0][a[i]],maxx[0][i]-sum[i+1]);
        chkmax(hlp0,hlp[0][a[i]]);
        chkmax(hlp[1][a[i]],maxx[1][i]-sum[i+1]);
        chkmax(hlp1,hlp[1][a[i]]);
    }
    printf("%lld\n",maxx[0][n]);
}
bool memEn;
void fl() {
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
}
int main() {
    debug("%.24lf\n",fabs(&memEn-&memBeg)/1024.0/1024.0);
    fl();
    int _=readint();
    while(_--) mian();
    return 0;
}
/*
g++ color.cpp -o color -std=c++14 -O2 -Wall -Wextra -Wshadow
# -fsanitize=address,undefined
*/